//
//  CJCustomAlertDemicTextView.m
//  DatangLife
//
//  Created by 陈建 on 2018/5/28.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomAlertDemicTextView.h"

#define DEFAULT_MAX_HEIGHT kScreenHeight/3*2




#define descriptionFont 16 //更新内容显示字体大小
#define animationTimeInterval 0.6f //入场时间
@interface CJCustomAlertDemicTextView ()<UITextViewDelegate>

{
    NSString *_textSting;
}

/**
 标题
 */
@property (nonatomic, copy) NSString *titleString;
@property (nonatomic, strong) UITextView *desTextView;

@end


@implementation CJCustomAlertDemicTextView





-(instancetype)initWithTitle:(NSString *)titleString{
    self = [super init];
    if (self) {
        
        self.titleString = titleString;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    //获取更新内容高度
//    CGFloat descHeight = [self sizeOfString:self.desc font:[UIFont systemFontOfSize:descriptionFont] maxSize:CGSizeMake(self.frame.size.width - Ratio(80) - Ratio(56), MAXFLOAT)].height;
    
    
    
    //bgView实际高度
//    CGFloat realHeight = descHeight + Ratio(248);
    
    //bgView最大高度
    CGFloat maxHeight = DEFAULT_MAX_HEIGHT;
    //更新内容可否滑动显示
//    BOOL scrollEnabled = NO;
    
//    //重置bgView最大高度 设置更新内容可否滑动显示
//    if (realHeight > DEFAULT_MAX_HEIGHT) {
//        scrollEnabled = YES;
//        descHeight = DEFAULT_MAX_HEIGHT - Ratio(248);
//    }else
//    {
//        maxHeight = realHeight;
//    }
//
    //backgroundView
    
    CGFloat titleLableH = kSize(16);
    CGFloat space = kSize(15);
    CGFloat textViewH = kSize(40);
    CGFloat btnH = kSize(28);
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = kWhiteColor;
    bgView.center = self.center;
    bgView.bounds = CGRectMake(0, 0, self.frame.size.width - kSize(100), titleLableH + textViewH + btnH + space *4);
    bgView.layerCornerRadius = 8;
    [self addSubview:bgView];
    
    //添加title
    UILabel *titlelable = [[UILabel alloc] initWithFrame:CGRectMake(0, space, bgView.width, titleLableH)];
    titlelable.textAlignment = NSTextAlignmentCenter;
    titlelable.font = kFont(16.f);
    titlelable.textColor = [UIColor colorWithHexString:kNomalTextBlackColor];
    titlelable.text = self.titleString;
    [bgView addSubview:titlelable];
    
    
    
    
    //输入内容
    UITextView *descTextView = [[UITextView alloc]initWithFrame:CGRectMake(space, CGRectGetMaxY(titlelable.frame)+space, bgView.width - 2*space, textViewH)];
    descTextView.font = [UIFont systemFontOfSize:descriptionFont];
    descTextView.textContainer.lineFragmentPadding = 0;
    descTextView.delegate = self;
//    descTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    descTextView.layer.borderColor = [UIColor colorWithHexString:kDetailTextGrayBlackColor].CGColor;
    descTextView.layer.borderWidth = 0.5;
    
    descTextView.scrollEnabled = YES;

    [bgView addSubview:descTextView];
    self.desTextView = descTextView;
   
    //更新按钮
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancleButton.backgroundColor = [UIColor colorWithHexString:kDetailTextGrayBlackColor];
    cancleButton.frame = CGRectMake(space, CGRectGetMaxY(descTextView.frame)+space, bgView.width/2 - 2*space, btnH);
    
    cancleButton.clipsToBounds = YES;
    cancleButton.layer.cornerRadius = 2.0f;
    [cancleButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:cancleButton];
    
    //取消按钮
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sureButton.frame = CGRectMake(CGRectGetMaxX(cancleButton.frame)+2*space, CGRectGetMaxY(descTextView.frame)+space, cancleButton.width, btnH);
  
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.backgroundColor = kCommonRedColor;
    
    [sureButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [bgView addSubview:sureButton];
   
    
    cancleButton.layerCornerRadius = sureButton.layerCornerRadius = 6;
    //显示
    [self showWithAlert:bgView];
    
}

- (void)textViewDidChange:(UITextView *)textView{
    _textSting = textView.text;
}

/**
 确定按钮处理
 */
- (void)sureButton{
    [self.desTextView endEditing:YES];
   
    if ([CJUIKit isNullWithString:_textSting]) {
        [CJUIKit showErrorWithStatus:@"请输入邀请码"];
    }else{
        [self dismaissAlert];
        self.sureBtnClick(_textSting);
    }
    
}


/**
 取消按钮点击事件
 */
- (void)cancelAction{
    [self dismaissAlert];
}

- (void)dismaissAlert{
    [UIView animateWithDuration:animationTimeInterval animations:^{
        self.transform = (CGAffineTransformMakeScale(1.5, 1.5));
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

/**
 添加alert入场动画
 
 @param alert 添加动画的View
 */
- (void)showWithAlert:(UIView *)alert{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = animationTimeInterval;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [alert.layer addAnimation:animation forKey:nil];
}




- (CGSize)sizeOfString:(NSString *)string font:(UIFont *)font maxSize:(CGSize)maxSize{
    return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
}


@end
